<% add_decidim_page_title(t(".title")) %>
<div class="card">
  <div class="item_show__header item_show__header--with-action-options">
    <h1 class="item_show__header-title">
      <%= t(".title") %>
      <%= link_to t("actions.new_survey", scope: "decidim.surveys"), new_survey_path, class: "button button__sm button__secondary" %>
      <%= render partial: "decidim/admin/components/resource_action" %>
    </h1>
  </div>
  <div class="table-stacked mt-8">
    <table class="table-list">
      <thead>
        <tr>
          <th><%= t("models.survey.fields.title", scope: "decidim.surveys") %></th>
          <th><%= t("models.survey.fields.questions", scope: "decidim.surveys") %></th>
          <th><%= t("models.survey.fields.responses", scope: "decidim.surveys") %></th>
          <th><%= t("models.survey.fields.status", scope: "decidim.surveys") %></th>
          <th><%= t("models.survey.fields.published", scope: "decidim.surveys") %></th>
          <th><%= t("actions.title", scope: "decidim.surveys") %></th>
        </tr>
      </thead>
      <tbody>
        <% surveys.each do |survey| %>
          <tr>
            <td data-label="<%= t("models.survey.fields.title", scope: "decidim.surveys") %>">
              <%= link_to decidim_sanitize_translated(survey.title), edit_survey_path(survey) %>
            </td>
            <td data-label="<%= t("models.survey.fields.questions", scope: "decidim.surveys") %>">
              <%= survey.questionnaire.questions.not_separator.size %>
            </td>
            <td data-label="<%= t("models.survey.fields.responses", scope: "decidim.surveys") %>">
              <%= survey.questionnaire.count_participants %>
            </td>
            <td data-label="<%= t("models.survey.fields.status", scope: "decidim.surveys") %>">
              <%= survey.open? ? t("models.survey.status.open", scope: "decidim.surveys") : t("models.survey.status.closed", scope: "decidim.surveys") %>
            </td>
            <td data-label="<%= t("models.survey.fields.published", scope: "decidim.surveys") %>">
              <% if survey.published? %>
                <span class="label success !text-sm">
                  <%= t("models.survey.published.published", scope: "decidim.surveys") %>
                </span>
              <% else %>
                <span class="label alert !text-sm">
                  <%= t("models.survey.published.unpublished", scope: "decidim.surveys") %>
                </span>
              <% end %>
            </td>
            <td data-label="<%= t("actions.title", scope: "decidim.surveys") %>" class="table-list__actions">
              <button type="button" data-controller="dropdown" data-target="actions-survey-<%= survey.id %>" aria-label="<%= t("decidim.admin.actions.actions_label", resource: survey.title) %>">
                <%= icon "more-fill", class: "text-secondary" %>
              </button>

              <div class="inline-block relative">
                <ul id="actions-survey-<%= survey.id %>" class="dropdown dropdown__action" aria-hidden="true">
                  <li class="dropdown__item">
                    <%= link_to edit_survey_path(survey), class: "dropdown__button" do %>
                      <%= icon "pencil-line" %>
                      <%= t("actions.edit", scope: "decidim.surveys") %>
                    <% end %>
                  </li>

                  <li class="dropdown__item">
                    <%= link_to edit_questions_questions_survey_path(survey), class: "dropdown__button" do %>
                      <%= icon "survey-line" %>
                      <%= t("actions.manage_questions", scope: "decidim.surveys") %>
                    <% end %>
                  </li>

                  <hr>

                  <% if allowed_to?(:update, :questionnaire) %>
                    <% if survey.published? %>
                      <li class="dropdown__item">
                        <%= link_to unpublish_survey_path(survey), method: :put, data: { confirm: t("actions.confirm_unpublish_survey", scope: "decidim.admin") }, class: "dropdown__button" do %>
                          <%= icon "close-circle-line" %>
                          <%= t("actions.unpublish", scope: "decidim.admin") %>
                        <% end %>
                      </li>
                    <% elsif survey.clean_after_publish? %>
                      <li class="dropdown__item">
                        <%= link_to publish_survey_path(survey), method: :put, data: { confirm: t("actions.responses_alert", scope: "decidim.surveys", responses_count: survey.questionnaire.responses.size) }, class: "dropdown__button" do %>
                          <%= icon "check-line" %>
                          <%= t("actions.publish", scope: "decidim.admin") %>
                        <% end %>
                      </li>
                    <% else %>
                      <li class="dropdown__item">
                        <%= link_to publish_survey_path(survey), method: :put, class: "dropdown__button" do %>
                          <%= icon "check-line" %>
                          <%= t("actions.publish", scope: "decidim.admin") %>
                        <% end %>
                      </li>
                    <% end %>

                    <hr>
                  <% end %>

                  <% if allowed_to? :preview, :questionnaire %>
                    <li class="dropdown__item">
                      <%= link_to resource_locator(survey).path, target: :blank, data: { "external-link": false }, class: "dropdown__button" do %>
                        <%= icon "eye-line" %>
                        <%= t("actions.preview", scope: "decidim.surveys") %>
                      <% end %>
                    </li>

                    <hr>
                  <% end %>

                  <li class="dropdown__item">
                    <%= dropdown_resource_permissions_link(survey) %>
                  </li>

                  <hr>

                  <li class="dropdown__item">
                    <%= link_to survey_path(survey), method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.surveys") }, class: "dropdown__button" do %>
                      <%= icon "delete-bin-line" %>
                      <%= t("actions.destroy", scope: "decidim.surveys") %>
                    <% end %>
                  </li>
                </ul>
              </div>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
</div>
